use std::{
    fs, io::{self, Write}, net::{TcpListener, TcpStream}, thread, time::Duration
};

fn main(){

    let conn = TcpListener::bind("127.0.0.1:7878").unwrap();

    // let threadPool = ThreadPool::new(8);

    for stream in conn.incoming() {
        let stream = stream.unwrap();

        thread::spawn(||handle_connection(stream));
        // threadPool.execute(||{
        //     handle_connection(stream);
        // });
    }

}

fn handle_connection(mut stream: TcpStream) {


    thread::sleep(Duration::from_secs(5));

    let status_line = "HTTP/1.1 200 OK";
    let contents = fs::read_to_string("index.html").unwrap();
    let length = contents.len();
    let response = format!("{status_line}\r\nContent-Length: {length}\r\n\r\n{contents}");

    stream.write(response.as_bytes()).unwrap();
}